package com.aly.beanValidate.core.validator.impl;

import com.aly.beanValidate.core.FieldDefinition;
import com.aly.beanValidate.core.GlobalConfigs;
import com.aly.beanValidate.core.Util;
import com.aly.beanValidate.core.validator.CheckValidator;
import com.aly.beanValidate.core.validator.Name;
import com.aly.beanValidate.i.BeanInfo;
import com.aly.beanValidate.i.FileInfo;
import com.aly.beanValidate.i.FileInfos;
import org.dom4j.Element;

@Name("GlobalValidatorRef")
public class RefGlobalValidator extends CheckValidator {
    private CheckValidator global;

    RefGlobalValidator(Element element, GlobalConfigs globalConfigs) {
        super(element, globalConfigs);
        String globalValidator = Util.attr(element, "ref");
        global = globalConfigs.getValidators(globalValidator);
        if (global == null) {
            throw new RuntimeException(" 未找到 全局配置 的校验器/或者请检查配置顺序:" + globalValidator);
        }
    }

    @Override
    public StringBuffer doValidate(FieldDefinition field, Object val, BeanInfo bean, FileInfo fileInfo, FileInfos fileInfos) {
        return global.doValidate(field, val, bean, fileInfo, fileInfos);
    }
}
